package personal.zq.generator.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import personal.zq.generator.sys.client.req.UserInfoReq;
import personal.zq.generator.sys.client.resp.UserInfoResp;
import personal.zq.generator.sys.dao.dataservice.UserInfoDataService;
import personal.zq.generator.sys.dao.entity.UserInfo;
import personal.zq.generator.sys.service.UserInfoService;
import personal.zq.generator.sys.service.utils.PageUtils;

/**
 * 用户信息 服务实现类
 *
 * @author zhangqing
 * @since 2022-06-28 12:40:26
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoDataService dataService;

    @SneakyThrows
    @Override
    public void create(UserInfoReq req) {
        UserInfo dto = UserInfo.class.newInstance();
        BeanUtils.copyProperties(req, dto);
        dataService.save(dto);
    }

    @SneakyThrows
    @Override
    public void update(UserInfoReq req) {
        UserInfo dto = UserInfo.class.newInstance();
        BeanUtils.copyProperties(req, dto);
        dataService.updateById(dto);
    }

    @Override
    public void del(Long id) {
        dataService.removeById(id);
    }

    @SneakyThrows
    @Override
    public IPage
            <UserInfoResp> qry(UserInfoReq req) {
        UserInfo dto = UserInfo.class.newInstance();
        BeanUtils.copyProperties(req, dto);
        QueryWrapper queryWrapper = new QueryWrapper();
        IPage<UserInfo> iPage = new Page<>(req.getCurrentPage(), req.getPageSize());
        iPage = dataService.page(iPage, queryWrapper);
        return PageUtils.convertPage(iPage, UserInfoResp.class);
    }

    @SneakyThrows
    @Override
    public UserInfoResp getDetail(Long id) {
        UserInfo dto = dataService.getById(id);
        UserInfoResp resp = UserInfoResp.class.newInstance();
        BeanUtils.copyProperties(dto, resp);
        return resp;
    }
}
